using System.Collections.Generic;
using Pegasus.Runtime.Serialization;
using RollManager.Library.ActionSteps;

namespace RollManager.Library
{
    public class SubAction : PermissionsItemBase
    {
        public int ActionId;
        public int ServerGroupId;
        public string Steps;

        private List<ActionStep> m_stepList = null;

        public List<ActionStep> StepList
        {
            get
            {
                if( m_stepList == null )
                {
                    m_stepList = (List<ActionStep>) SerializationHelper.XmlToObject( Steps );
                    if( m_stepList == null )
                    {
                        m_stepList = new List<ActionStep>();
                    }
                }

                return m_stepList;
            }
            set
            {
                m_stepList = value;
                Steps = SerializationHelper.ObjectToXml( value );
            }
        }

        public SubAction Clone( List<ActionStep> steps, string formatName )
        {
            SubAction subAction = new SubAction();
            CopyTo( subAction );
            subAction.Id = 0;   // Vital to make sure sub action is not logged.

            subAction.ActionId = ActionId;
            subAction.StepList = steps;
            subAction.Name = string.Format( "{0} History", Name );
            subAction.ServerGroupId = ServerGroupId;

            return subAction;
        }

        public SubAction Clone()
        {
            SubAction copy = new SubAction();
            CopyTo( copy );

            copy.ActionId = ActionId;
            copy.ServerGroupId = ServerGroupId;
            copy.Steps = Steps;

            return copy;
        }
    }
}
